<?php

namespace ctrl;

use model\video_cate;
use model\videos;
use model\siteconfig;
use libs\view;

class video extends base
{
	private static $M, $MS, $upConfig;
	public static function Init()
	{
		parent::init();
		self::$M = new videos;
		self::$MS = new video_cate;
		$config =  new siteconfig;
		self::$upConfig = $config->getUpload();
	}
	public static function index()
	{
		if (isset($_GET['list_json'])) {
			jsonList(self::$M->jsonPageSelect());
		} else {
			view::assign('catelist', self::$MS->SelectData());
			view::assign('cid', $_GET['cid'] ?? '');
			view::display();
		}
	}

	public static function add()
	{
		if (isset($_GET['view'])) {
			view::assign('upload', self::$upConfig); //获取上传类型
			view::assign('catelist', self::$MS->SelectData());
			view::assign('info', null);
			view::assign('title', "添加视频");
			view::display("post");
		} else {
			$result = self::$M->insertData();
			Response(array('status' => $result['status'], 'info' => $result['msg'], 'cid' => $result['cid']));
		}
	}

	public static function edit()
	{
		if (isset($_GET['view'])) {
			view::assign('upload', self::$upConfig); //获取上传类型
			view::assign('catelist', self::$MS->SelectData());
			view::assign('info', self::$M->findData());
			view::assign('title', "编辑视频");
			view::display("post");
		} elseif (isset($_GET['switchStatus'])) {
			if (self::$M->switchStatus()) Response(array('status' => 1, 'info' => '设置成功'));
		} else {
			$result = self::$M->updateData();
			Response(array('status' => $result['status'], 'info' => $result['msg'], 'cid' => $result['cid']));
		}
	}

	public static function del()
	{
		if (self::$M->deleteOneData())
			Response(array('status' => 1, 'info' => '删除成功'));
		else
			Response(array('status' => 0, 'info' => '删除错误!'));
	}
}
